package com.wangyonghao.harmonyocrs.dao;

import com.wangyonghao.harmonyocrs.pojo.RecogHistory;
import com.wangyonghao.harmonyocrs.utils.DateUtils;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;

import java.util.ArrayList;
import java.util.List;

public class RecogHistoryDao {
    DatabaseHelper helper;
    RdbStore store;
    StoreConfig config = StoreConfig.newDefaultConfig("HarmonyCORS.db");
    private static RdbOpenCallback callback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            rdbStore.executeSql("create table if not exists recog_history " +
                    "(id integer primary key autoincrement,way text," +
                    "content text, recog_time text)");
            rdbStore.executeSql("insert into recog_history (id,way,content,recog_time)" +
                    "values(10001,'picture','数据库被创建时回调,并返回创建好的数据库，一般也在oncreate方法里面做一些初始化数据表的操作','2022-02-23 11:13:14')");
            rdbStore.executeSql("insert into recog_history (id,way,content,recog_time)" +
                    "values(10002,'camera','在 “隐藏文件夹” 里找到 8 个视频','2022-02-23 11:13:14')");
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {

        }
    };

    public RecogHistoryDao(DatabaseHelper helper) {
        this.helper = helper;
        store = helper.getRdbStore(config, 1, callback, null);
    }


    public long addRecogHistory(RecogHistory recogHistory) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putInteger("id", recogHistory.getId());
        bucket.putString("way", recogHistory.getWay());
        bucket.putString("content", recogHistory.getContent());
        bucket.putString("recog_time", DateUtils.datetimeToString(DateUtils.now()));
        return store.insert("recog_history", bucket);
    }

    public RecogHistory findRecogHistoryById(Integer id) {
        ResultSet resultSet = store.querySql("select id,way,content,recog_time from recog_history where id = ?", new String[]{String.valueOf(id)});
        resultSet.goToFirstRow();
        RecogHistory recogHistory = new RecogHistory();
        recogHistory.setId(resultSet.getInt(0));
        recogHistory.setWay(resultSet.getString(1));
        recogHistory.setContent(resultSet.getString(2));
        recogHistory.setRecog_time(DateUtils.stringToDate(resultSet.getString(3)));
        return recogHistory;
    }

    public List<RecogHistory> findRecogHistoryByKey(String key) {
        //*** sqlite实现模糊查询:store.querySql(new RdbPredicates("recog_history").like("content",content),co)
        String[] columns = new String[]{"id", "way", "content", "recog_time"};
        ResultSet resultSet = null;
        if (key == null || "".equals(key)) {
            resultSet = store.query(new RdbPredicates("recog_history"), columns);
        } else {
            resultSet = store.query(new RdbPredicates("recog_history").like("content", key), columns);
        }
        List<RecogHistory> recogHistories = new ArrayList<>();
        resultSet.goToFirstRow();
        while (!resultSet.isEnded()) {
            RecogHistory recogHistory = new RecogHistory();
            recogHistory.setId(resultSet.getInt(0));
            recogHistory.setWay(resultSet.getString(1));
            recogHistory.setContent(resultSet.getString(2));
            recogHistory.setRecog_time(DateUtils.stringToDate(resultSet.getString(3)));
            recogHistories.add(recogHistory);
            resultSet.goToNextRow();
        }
        return recogHistories;
    }

    public int deleteRecogHistoryById(Integer id) {
        return store.delete(new RdbPredicates("recog_history").equalTo("id", id));
    }
}
